<template>
  <div id="addDetail">
    <a-card>
      <a-row  style="margin-bottom: 16px;">
        <a-col flex="auto" class="content-wrapper">
          <h2>{{ data.appName }}</h2>
          <p>应用描述: {{ data.appDesc }}</p>
          <p>应用类型: {{ APP_TYPE_MAP[data.appType] }}</p>
          <p>评分策略: {{ APP_SCORING_STRATEGY_MAP[data.scoringStrategy] }}</p>
          <p>
            <a-space>
              作者: 
              <div
                :style="{ display: 'flex', alignItems: 'center',}"
              >
                <a-avatar :image-url="data.user?.userAvatar" :size="24" :style="{ marginRight: '8px' }" />
                <a-typography-text>{{data.user?.userName ?? '匿名用户'}}</a-typography-text>
              </div>
            </a-space>
          </p>
          <p>创建时间: {{ dayjs(data.createTime).format('YYYY-MM-DD HH:mm:ss') }}</p>
          <a-space size="medium">
            <a-button type="primary" @click="toOthers('doQuestion')">开始答题</a-button>
            <a-button @click="doShare">分享应用</a-button>
            <a-button v-if="isMy" @click="toOthers('question')">设置题目</a-button>
            <a-button v-if="isMy" @click="toOthers('score')">设置评分</a-button>
            <a-button v-if="isMy" @click="toOthers('app')">修改应用</a-button>
          </a-space>
        </a-col>
        <a-col flex="320px">
          <!-- 这里的width封装了一层,是父元素的100% -->
          <a-image width="100%" :src="data.appIcon" />
        </a-col>
      </a-row>
    </a-card>
    <!-- {{ data }} -->
        <!-- 弹框分享组件 -->
   <ShareModel :link="link" :title="title" ref="shareModel"/>
  </div>
</template>

<script setup lang="ts">
import ShareModel from '@/components/ShareModel.vue';
import { ref, watchEffect, computed } from 'vue'
import API from '@/api'
import message from '@arco-design/web-vue/es/message'
import { getAppVoByIdUsingGet } from '@/api/appController';
import { dayjs } from '@arco-design/web-vue/es/_utils/date'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/store/userStore';
import { APP_SCORING_STRATEGY_MAP, APP_TYPE_MAP } from '@/constant/app';
interface Props {
  id: string 
}

// 根据id获取应用
const props = withDefaults(defineProps<Props>(),{
  id:  () => {
    return ""
  }
}) 

const data = ref<API.AppVO>({});

const loadData = async () => {
  if(!props.id) return
  const res = await getAppVoByIdUsingGet({
    id: props.id as any,
    // id: 1
  });
  if(res.data.code === 0) {
    data.value = res.data.data as any;
  } else {
    message.error('获取数据失败:' +res.data.message)
  }
}

// 判断是否是本人创建的题目
const loginUserStore = useLoginUserStore()

const loginUserId = loginUserStore.loginUser?.id

// 如果id为空,因为登录失败会给默认loginUser
// 为什么用computed,因为开始进入页面,可能还没获取到loginUser,后续再获取到,值不会变化
const isMy = computed(() => {
  return loginUserId && loginUserId === data.value.userId
})

const router = useRouter()
const toOthers = (str:string) => {
  if(str === 'question') {

    router.push(`/add/question/${props.id}`)

  } else if(str === 'score') {

    router.push(`/add/scroing_result/${props.id}`)

  } else if(str === 'app') {

    router.push(`/add/app/${props.id}`)

  } else if(str === 'doQuestion') {
    router.push(`/answer/do/${props.id}`)
  }
}

// 分享
const shareModel = ref()
const link = ref('https://www.baidu.com/')
const title = ref('分享应用')
const doShare = () => {
  shareModel.value.openModel()
}

watchEffect(() => {
  loadData()
})

</script>


<style lang="scss" scoped>
#addDetail {
  .content-wrapper > *{
    margin-bottom: 24px;
  }
}
</style>